package com.yq.bdm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.bdm.controller.common.AbstractController;
import com.yq.bdm.entity.UserInfoEntity;
import com.yq.bdm.service.UserInfoService;
import com.yq.bdm.utils.PageUtils;
import com.yq.bdm.utils.ResultMap;

/**
 * 用户表
 * 
 * @author yuq
 * @date 2017-07-21 16:35:14
 */
@Controller
@RequestMapping("/userinfo")
public class UserInfoController extends AbstractController {

	@Autowired
	private UserInfoService userInfoService;

	@RequestMapping("/index.html")
	public String list() {
		return "admin/userinfo/userinfo.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public ResultMap list(String id, String userId, String loginName,
			String password, String userName, String mobile, String email,
			String lastLoginTime, String loginCount, String isEffective,
			String gmtCreate, String gmtModified) {

		Map<String, Object> map = new HashMap<>();
		// 查询条件
		map.put("id", id);
		map.put("userId", userId);
		map.put("loginName", loginName);
		map.put("password", password);
		map.put("userName", userName);
		map.put("mobile", mobile);
		map.put("email", email);
		map.put("lastLoginTime", lastLoginTime);
		map.put("loginCount", loginCount);
		map.put("isEffective", isEffective);
		map.put("gmtCreate", gmtCreate);
		map.put("gmtModified", gmtModified);

		// 获取分页参数，添加到查询请求参数中
		getPage(map);

		// 查询列表数据
		List<UserInfoEntity> userInfoList = userInfoService.queryList(map);
		int total = userInfoService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(userInfoList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}

	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("/goAdd")
	public String goAdd() {
		return "admin/userinfo/add.html";
	}

	/**
	 * 跳转到编辑页面
	 */
	@RequestMapping("/goEdit/{id}")
	public String goEdit(@PathVariable("id") Long id) {
		// 设置当前control的访问url，edit页面需要使用
		request.setAttribute("namespaceUrl",
				request.getContextPath() + "/userinfo");
		UserInfoEntity userInfo = userInfoService.queryObject(id);
		request.setAttribute("userInfo", userInfo);
		return "admin/userinfo/edit.html";
	}

	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	public ResultMap info(@PathVariable("id") Long id) {
		UserInfoEntity userInfo = userInfoService.queryObject(id);

		return ResultMap.ok().put("userInfo", userInfo);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public ResultMap save(@RequestBody UserInfoEntity userInfo) {
		userInfoService.save(userInfo);

		return ResultMap.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public ResultMap update(@RequestBody UserInfoEntity userInfo) {
		userInfoService.update(userInfo);

		return ResultMap.ok();
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public ResultMap delete(@RequestBody Long[] ids) {
		userInfoService.deleteBatch(ids);

		return ResultMap.ok();
	}

	/**
	 * 删除成功后，查询列表的数据
	 */
	@ResponseBody
	@RequestMapping("/deleteAfterList")
	public ResultMap deleteAfterList(@RequestBody Long[] ids) {
		userInfoService.deleteBatch(ids);

		Map<String, Object> map = new HashMap<>();
		// 获取分页参数，添加到查询请求参数中
		getPage(map);
		// 查询列表数据
		List<UserInfoEntity> userInfoList = userInfoService.queryList(map);
		int total = userInfoService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(userInfoList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
}
